package com.inxedu.os.edu.controller.systemMsg;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.inxedu.os.common.ObjectUtils;
import com.inxedu.os.edu.pojo.EduMsgSystem;
import com.inxedu.os.edu.pojo.EduUser;
import com.inxedu.os.edu.pojo.InxeduResult;
import com.inxedu.os.edu.pojo.MsgSystem;
import com.inxedu.os.edu.service.systemMsg.MsgReceiveService;
import com.inxedu.os.edu.service.systemMsg.MsgSystemService;
import com.inxedu.os.edu.service.systemMsg.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Controller
@RequestMapping("/admin")
@Api(value = "系统消息接口",tags = "系统消息接口")
public class MsgSystemController {
    private Log logger= LogFactory.getLog(MsgSystemController.class);
    @Autowired
    private MsgSystemService msgSystemService;
    @Autowired
    private UserService userService;
    @Autowired
    private MsgReceiveService msgReceiveService;
    /**
     * 查询系统消息列表
     * @return
     */
    @RequestMapping(value = "/letter/systemmsglist",method = {RequestMethod.GET,RequestMethod.POST})
    @ApiOperation(value = "查询系统消息列表",notes = "查询系统消息列表信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "content",value = "内容",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "addTime",value = "开始时间",dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "endTime",value = "结束时间",dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "status",value = "状态",dataType = "Byte",paramType = "query")})
    public String systemMsgList(HttpServletRequest request,@ModelAttribute("msgSystem") MsgSystem msgSystem,@RequestParam(defaultValue = "1")int page){
        PageHelper.startPage(page,10);
        List<EduMsgSystem> msgSystemList = msgSystemService.getMsgSystemList(msgSystem);
        PageInfo<EduMsgSystem> pageInfo=new PageInfo<>(msgSystemList);
        request.setAttribute("page",pageInfo);
        request.setAttribute("nsgSystemList",msgSystemList);
        return "/view/inxedu/admin/letter/to_msg_system_list";
    }

    /**
     *跳转到发消息页面
     */
    @RequestMapping(value = "/user/letter/toSendSystemMessages",method = {RequestMethod.GET,RequestMethod.POST})
    @ApiOperation(value = "跳转到发消息页面",notes = "跳转到发消息页面")
    public String toSystemMessages(){
        return "/view/inxedu/admin/user/to_send_systemMessage";
    }
    /**
     * 发送系统消息
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/user/letter/sendJoinGroup",method = {RequestMethod.GET,RequestMethod.POST})
    @ApiOperation(value = "发送系统消息",notes = "发送系统消息")
    @ResponseBody
    public Map<String, Object> sendSystemMessages(HttpServletRequest request){
        Map<String,Object> map=new HashMap<>();
        try{
            String content = request.getParameter("content");
            EduMsgSystem msgSystem=new EduMsgSystem();
            msgSystem.setUpdateTime(new Date());
            msgSystem.setAddTime(new Date());
            msgSystem.setContent(content);
            msgSystemService.addMsgSystem(msgSystem);
            map.put("message", "success");
        }catch (Exception e){
            logger.info("sendMessage error");
            System.out.println(e.getMessage());
        }
        return map;
    }

    /**
     * 跳转到 批量  发消息页面
     *
     * @param
     * @return
     */
    @ApiOperation(value = "跳转到批量发消息页面",notes = "跳转到批量发消息页面")
    @RequestMapping(value = "/user/letter/toSendSystemMessagesBatch",method = {RequestMethod.GET,RequestMethod.POST})
    public String toSendSystemMessagesBatch(){
        return "/view/inxedu/admin/user/to_send_systemMessage_batch";
    }
    /**
     * 批量发送系统消息
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/user/letter/sendJoinGroupBatch",method = {RequestMethod.GET,RequestMethod.POST})
    @ApiOperation(value = "批量发送系统消息",notes = "批量发送系统消息")
    @ResponseBody
    public Map<String,Object> sendJoinGroupBatch(HttpServletRequest Request){
        Map<String,Object> map=new HashMap<>();
        try{
            String userEmails = Request.getParameter("userEmails");
            String content = Request.getParameter("content");
            if(ObjectUtils.isNull(userEmails)){
                map.put("message", "用户邮箱不能为空");
                return map;
            }
            String[] emails = userEmails.split(",");
            for(int i=0;i<emails.length;i++){
                if(ObjectUtils.isNotNull(emails[i].trim())){
                    EduUser eduUser = userService.queryUserByEmailOrMobile(emails[i].trim());
                    if(ObjectUtils.isNull(eduUser)){
                        map.put("message","输入邮箱不正确，请重新输入");
                    }else {
                        msgReceiveService.addSystemMessageByCusId(content,Long.valueOf(eduUser.getUserId()));
                        map.put("message", "success");
                    }
                }
            }
        }catch (Exception e){
            logger.info("receiveMessage error");
            System.out.println(e.getMessage());
            map.put("message", "系统错误,请稍后重试");
        }
        return map;
    }


    /**
     * 批量删除系统消息
     */
    @RequestMapping(value = "/letter/delsystem",method = {RequestMethod.GET,RequestMethod.POST})
    @ApiOperation(value = "批量删除系统消息",notes = "批量删除系统消息")
    @ApiImplicitParam(name = "ids",value = "id字符串",dataType = "String",paramType = "query")
    @ResponseBody
    public InxeduResult deleteMsg(HttpServletRequest request, @RequestParam("ids")String ids){
        InxeduResult inxeduResult=new InxeduResult();
        try {
            String[] split = ids.split(",");
            List<Integer> list=new ArrayList<>();
            for (String string:split
                 ) {
                list.add(Integer.valueOf(string));
            }
            for (Integer id:list
                 ) {
                msgSystemService.deleteMsg(id);
            }
            inxeduResult.setSuccess(true);
            inxeduResult.setMessage("操作成功");
        }catch (Exception e){
            logger.info("删除错误");
            inxeduResult.setSuccess(false);
            inxeduResult.setMessage("系统繁忙,请稍后再试");
        }
        return inxeduResult;
    }
}
